<!DOCTYPE html>
<html>

<head>
    <title>注册</title>
    <style>
        body {
            background-color: #f5f5f5;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
        }

        .register-container {
            max-width: 400px;
            width: 90%;
            padding: 20px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 28px;
            font-weight: 600;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #555;
            font-weight: 500;
            font-size: 14px;
        }

        .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            border-color: #28a745;
            box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.1);
            outline: none;
        }

        .form-group input::placeholder {
            color: #adb5bd;
        }

        .error-message {
            color: #dc3545;
            margin-bottom: 15px;
            text-align: center;
            padding: 10px;
            background-color: #fff5f5;
            border-radius: 4px;
            font-size: 14px;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        button:hover {
            background-color: #218838;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(40, 167, 69, 0.15);
        }

        button:active {
            transform: translateY(0);
        }

        .login-link {
            text-align: center;
            margin-top: 15px;
        }

        .login-link a {
            color: #28a745;
            text-decoration: none;
        }

        .login-link a:hover {
            text-decoration: underline;
        }

        /* 密码框容器样式 */
        .password-container {
            position: relative;
            width: 100%;
        }

        /* 眼睛图标样式 */
        .password-toggle {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #666;
            padding: 5px;
            z-index: 1;
        }

        .password-toggle:hover {
            color: #333;
        }
    </style>
</head>

<body>
    <div class="register-container">
        <h1>注册</h1>
        <% if (locals.error) { %>
            <div class="error-message">
                <%= error %>
            </div>
            <% } %>
                <form action="/register" method="POST">
                    <div class="form-group">
                        <label>用户名：</label>
                        <input type="text" name="username" required placeholder="4-16位字母、数字或下划线"
                            pattern="[a-zA-Z0-9_]{4,16}">
                    </div>
                    <div class="form-group">
                        <label>密码：</label>
                        <div class="password-container">
                            <input type="password" name="password" required placeholder="8-20位，包含大小写字母、数字和特殊字符"
                                pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,20}">
                            <span class="password-toggle" onclick="togglePassword(this)">👁️</span>
                        </div>
                    </div>
                    <button type="submit">注册</button>
                </form>
                <div class="login-link">
                    <p>已有账号？<a href="/login">登录</a></p>
                </div>
    </div>

    <script>
        const validatePassword = (password) => {
            // 至少8位，包含大小写字母、数字和特殊字符
            const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,20}$/;
            return regex.test(password);
        };

        const validateUsername = (username) => {
            // 4-16位，只能包含字母、数字和下划线
            const regex = /^[a-zA-Z0-9_]{4,16}$/;
            return regex.test(username);
        };

        function validateForm() {
            const username = document.querySelector('input[name="username"]').value.trim();
            const password = document.querySelector('input[name="password"]').value.trim();

            if (!validateUsername(username)) {
                alert('用户名必须是4-16位的字母、数字或下划线！');
                return false;
            }

            if (!validatePassword(password)) {
                alert('密码必须是8-20位，包含大小写字母、数字和特殊字符(!@#$%^&*)！');
                return false;
            }
            return true;
        }

        function togglePassword(element) {
            const input = element.previousElementSibling;
            if (input.type === 'password') {
                input.type = 'text';
                element.textContent = '👁️‍🗨️';
            } else {
                input.type = 'password';
                element.textContent = '👁️';
            }
        }
    </script>
</body>

</html>